package com.gopay.bis.cmbc.ebank.operation;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcB2bMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;
import com.hitrust.B2B.CMBC.request.DirectPayRequest;
import com.hitrust.B2B.CMBC.request.entry.OrderDetailEntry;
import com.hitrust.B2B.CMBC.request.entry.OrderEntry;
import com.hitrust.B2B.CMBC.request.entry.PayInfoEntry;
import com.hitrust.B2B.CMBC.response.GenericResponse;
import com.hitrust.B2B.CMBC.util.SecurityUtil;
import com.hitrust.B2B.CMBC.util.XMLDocument;
/**
 * 民生B2B签名/验签类
 * @CerthelperCMBCB2B.java
 */
public class CmbcB2bCerthelper implements ICertHelper{
	
	private static Log logger = LogFactory.getLog(CmbcB2bCerthelper.class);
	/**
	 * 民生B2B交易签名
	 * 
	 * @param Object plainText
	 * @return Object
	 * @throws GopayException 
	 */
	public String sign(BaseMessage baseMessage) throws GopayException {		
		try {
			CmbcMessage message = (CmbcMessage) baseMessage;
			BankLogUtils.printB2BSignPlainLog(message);
			// 创建直接支付交易请求对象
			DirectPayRequest dpr = new DirectPayRequest();
			dpr.setMerchantTrnxNo(message.getOrderId());
			dpr.setTrnxCode(message.getTrnxCode());
			dpr.setResultNotifyURL(message.getResultNotifyURL());
			dpr.setSignFlag(message.getSignFlag());
			dpr.setMerchantNo(message.getMerchantNo());
			// 创建支付信息对象
			PayInfoEntry payInfo = new PayInfoEntry();
			payInfo.setTrnxAmount(message.getAmount().toString());
			payInfo.setDigest(message.getDigest());
			payInfo.setPayerCustNo(message.getCustomerID());
			// 创建订单信息对象
			OrderEntry order = new OrderEntry();
			order.setOrderNo(message.getOrderId());
			order.setOrderAmt(message.getAmount().toString());
			order.setOrderDate(message.getOrderDate());
			order.setSellerBank(message.getSellerBank());
			order.setSellerBankAddress(message.getSellerBankAddress());
			order.setSellerCorporationAcctNo(message.getSellerCorporationAcctNo());
			order.setSellerCorporationAcctName(message.getSellerCorporationAcctName());
			order.setSellerCorporationName(message.getSellerCorporationName());
			order.setPayType(message.getPayType());
			// 创建订单明细对象
			OrderDetailEntry detail = new OrderDetailEntry();
			detail.setOperName(message.getOperName());
			order.setOrderDetail(detail);
			dpr.setOrder(order);
			dpr.setPayInfo(payInfo);
			// 创建应答结果对象
			GenericResponse res = null;
			res = dpr.postRequest();
			String rtnMsg = res.isSuccessed() + "|" + res.getMessage() + "|"+ res.getCode();		
			// 返回对象赋值
			message.setOrderStatus(String.valueOf(res.isSuccessed()));
			message.setResMessage(res.getMessage());
			message.setResCode(res.getCode());
			BankLogUtils.printB2BSignSignatureLog(baseMessage, rtnMsg);
			return rtnMsg;
		} catch (Exception e) {
			BankLogUtils.printB2BSignExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6028);
		}
	}

	/**
	 * 民生B2B交易验签
	 * 
	 * @param message
	 * @return rtnMsg
	 * @throws GopayException 
	 */
	public Object verify(BaseMessage baseMessage) throws GopayException {
		try {
			CmbcB2bMessage message = (CmbcB2bMessage) baseMessage;
			// 组装验签所需XMLDocument对象
			XMLDocument msgRes = new XMLDocument(message.getSignMessage());
			SecurityUtil.verifyNotifyMessage(msgRes);
			String orderNo = msgRes.getValueNoNull("OrderNo");
			String trnxAmount = msgRes.getValueNoNull("TrnxAmount");
			String stauts = msgRes.getValueNoNull("Status");
			String rtnMsg = orderNo + "|" + trnxAmount + "|" + stauts;
			BankLogUtils.printB2BVerifyPlainLog(message);
			BankLogUtils.printB2BVerifySignatureLog(message);
			BankLogUtils.printB2BVerifyResultLog(message, !rtnMsg.equals(""));
			return rtnMsg;
		}  catch (Exception e) {
			BankLogUtils.printB2BSignExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6030);
		}
	}

}